package com.personal.sfe.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.personal.sfe.user.entity.User;
import com.personal.sfe.user.mapper.UserMapper;
import com.personal.sfe.user.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 梁奔
 * @date: 2025/11/04
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public User getUserByUserName(String userName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEnabled, true);
        queryWrapper.eq(User::getUsername, userName);
        List<User> userList = list(queryWrapper);
        if(userList.size()>1){
            throw new IllegalStateException("Duplicate username found: "+userName);
        }
        return userList.stream().findFirst().orElse(null);
    }
}
